﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace MyLib.Web.UI {
	/// <summary>
	/// 
	/// </summary>
	[ToolboxData("<{0}:MyFormView runat=server></{0}:MyFormView>")]
	public class MyFormView : FormView {
		/// <summary>
		/// 
		/// </summary>
		/// <param name="e"></param>
		protected override void OnLoad(EventArgs e) {
			base.OnLoad(e);

			if (CurrentMode == FormViewMode.ReadOnly) {
				if (Page.IsPostBack) {
					DataBind();

					RequiresDataBinding = false;
				}
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected override void OnDataSourceViewChanged(object sender, EventArgs e) {
			if (Page.IsPostBack) {
				// Prevent re-databinding during postback.
				this.RequiresDataBinding = false;
			} else {
				base.OnDataSourceViewChanged(sender, e);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="e"></param>
		protected override void OnItemUpdated(FormViewUpdatedEventArgs e) {
			base.OnItemUpdated(e);

			e.KeepInEditMode = true;
		}

		/// <summary>
		/// 
		/// </summary>
		[DefaultValue(FormViewMode.Edit)]
		public override FormViewMode DefaultMode {
			get {
				return base.DefaultMode;
			}
			set {
				base.DefaultMode = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public MyFormView()
			: base() {
			DefaultMode = FormViewMode.Edit;
		}
	} // end of MyFormView.
}
